package com.vrv.app.cyberspace.modules.sys.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vrv.app.cyberspace.common.utils.JSONResult;
import com.vrv.app.cyberspace.modules.sys.entity.InfrastructureInformationTable;
import com.vrv.app.cyberspace.modules.sys.service.IInfrastructureInformationTableService;
import io.swagger.annotations.Api;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.util.PoiPublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

import static com.vrv.app.cyberspace.common.utils.JSONResult.renderError;
import static com.vrv.app.cyberspace.common.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sm
 * @since 2017-12-15
 */
@Api(description="信息基础设施")
@RestController
@RequestMapping("/sys/infrastructureInformationTable")
public class InfrastructureInformationTableController extends BaseController {

    @Autowired
    private IInfrastructureInformationTableService ifitservice;

    /**
     * 基础信息--新增/修改
     */
    @RequestMapping("/addorUpdate")
    public JSONResult addorUpdateInfrastructure(InfrastructureInformationTable it) {
        return ifitservice.insertOrUpdate(it) ? renderSuccess() : renderError();
    }

    /**
     * 基础信息--删除
     */
    @RequestMapping("/delete")
    public JSONResult deleteInfrastructure(InfrastructureInformationTable it) {
        return ifitservice.deleteById(it.getId()) ? renderSuccess() : renderError();
    }

    /**
     * 基础信息--查询
     */
    @RequestMapping("/select")
    public JSONResult selectInfrastructure(InfrastructureInformationTable it) {
        Wrapper<InfrastructureInformationTable> ew = new EntityWrapper<>();
        createCondition(it, ew);
        return JSONResult.renderSuccess(ifitservice.selectPage(new Page<>(it.getPage(), it.getSize()), ew));
    }

    private void createCondition(InfrastructureInformationTable it, Wrapper<InfrastructureInformationTable> ew) {
        if (it.getId() != null)
            ew.eq("id", it.getId());
        if (it.getName() != null)
            ew.like("name", it.getName());
        if (it.getUnit() != null)
            ew.like(true, "unit", it.getUnit());
        if (it.getLeadingOfficial() != null)
            ew.like(true, "leadingOfficial", it.getLeadingOfficial());
        if (it.getContactPhone() != null)
            ew.like(true, "contactPhone", it.getContactPhone());
        if (it.getIp() != null)
            ew.like(true, "ip", it.getIp());
        if (it.getDomainName() != null)
            ew.like(true, "domainName", it.getDomainName());
        if (it.getUrl() != null)
            ew.like(true, "url", it.getUrl());
    }

    /**
     * 基础信息分页列表
     */
    @RequestMapping("/list")
    public JSONResult listInfrastructure(InfrastructureInformationTable it) {
        return JSONResult.renderSuccess(ifitservice.selectPage(new Page<>(it.getPage(), it.getSize())));
    }

    /**
     * excel 批量导入
     */
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public JSONResult importExcel(MultipartHttpServletRequest request) {
        JSONResult jsonResult = JSONResult.renderSuccess();
        try {
            //得到上传的文件
            MultipartFile multipartFile = request.getFile("file");
            //转换为输入流
            InputStream in = multipartFile.getInputStream();
            //excel数据导入
            ImportParams params = new ImportParams();
            List<InfrastructureInformationTable> excelList = ExcelImportUtil.importExcel(in, InfrastructureInformationTable.class, params);
            List<InfrastructureInformationTable> list = ifitservice.selectList(new EntityWrapper<>());
            excelList.removeAll(list);
            if (excelList.size() > 0)
                ifitservice.insertOrUpdateBatch(excelList);
        } catch (Exception e) {
            jsonResult = JSONResult.renderError();
            e.printStackTrace();
        }
        return jsonResult;
    }


    /**
     * 基础设施信息模板下载
     */
    @RequestMapping(value = "template")
    public void downPhotoByStudentId(final HttpServletResponse response) throws IOException {
        InputStream in = new FileInputStream(PoiPublicUtil.getWebRootPath("excel/Infrastructure.xlsx"));
        byte[] body = new byte[in.available()];
        in.read(body);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=Infrastructure.xlsx");
        response.addHeader("Content-Length", "" + body.length);
        response.setContentType("application/octet-stream;charset=UTF-8");
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        outputStream.write(body);
        outputStream.flush();
        outputStream.close();
    }
}

